package com.sp.method;

public class ThreadMethod02 {
    public static void main(String[] args) throws InterruptedException {
        T2 t2 = new T2();
        t2.start();
        for (int i = 0; i < 20; i++) {
            Thread.sleep(1000);
            System.out.println("主线程吃了 " + i + "个包子");
            if (i == 5){
                System.out.println("主线程先停止吃包子");
                //join线程插队
                t2.join();//让这个t2线程插队先执行完

                //yield礼让, 不一定成功
//                Thread.yield();//让当前线程礼让, 但是不一定成功
                System.out.println("子线程吃完了包子, 主线程接着吃");
            }
        }
    }
}

class T2 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
            System.out.println("子线程吃了 " + i + "个包子");
        }
    }
}